<template>
	<div class="system-add-dept-container">
		<el-drawer title="修改系统字典" v-model="showWindows" @close="onCancel">
			<el-form ref="formRef" :model="sysDict" :rules="rules" size="small" label-width="30%" style="width: 90%">
				<el-form-item label="字典编码" prop="dicCode">
					<el-input v-model="sysDict.dicCode" placeholder="请输入字典编码" disabled />
				</el-form-item>
				<el-form-item label="字典名称" prop="dicName">
					<el-input v-model="sysDict.dicName" placeholder="请输入字典名称" />
				</el-form-item>
				<el-form-item label="是否只读" prop="platform">
					<el-switch v-model="sysDict.platform" disabled />
				</el-form-item>
				<div class="el-drawer__footer">
					<el-button @click="onCancel" size="small">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="small">确 定</el-button>
				</div>
			</el-form>
		</el-drawer>
	</div>
</template>

<script lang="ts">
import { reactive, ref, toRefs } from 'vue';
import { editDict, getDictDetail } from '/@/api/system/dict/index';
import { ElMessage } from 'element-plus';

export default {
	name: 'editDict',
	emits: ['initDictData'],
	props: {},
	setup(props, context) {
		const formRef = ref();

		const state = reactive({
			showWindows: false,
			sysDict: {
				dicId: '',
				dicCode: '',
				dicName: '',
				platform: false,
			},
			dicId: '',
			rules: {
				dicCode: [{ required: true, message: '字典编码不能为空', trigger: 'blur' }],
				dicName: [{ required: true, message: '字典名称不能为空', trigger: 'blur' }],
			},
		});

		//打开添加窗口
		const openWindows = (dictId: any) => {
			state.dicId = dictId;
			getDictDetail(dictId).then((res: any) => {
				state.sysDict.dicId = res.data.dicId;
				state.sysDict.dicCode = res.data.dicCode;
				state.sysDict.dicName = res.data.dicName;
				state.sysDict.platform = res.data.platform;
			});
			state.showWindows = true;
		};

		//取消
		const onCancel = () => {
			formRef.value.resetFields();
			state.showWindows = false;
		};

		//提交
		const onSubmit = () => {
			formRef.value.validate((valid: any) => {
				if (valid) {
					editDict(state.sysDict, state.dicId).then((res: any) => {
						if (res.code == 200) {
							onCancel();
							context.emit('initDictData');
						} else {
							ElMessage.error(res.message);
						}
					});
				}
			});
		};

		return {
			formRef,
			openWindows,
			onCancel,
			onSubmit,
			...toRefs(state),
		};
	},
};
</script>

<style scoped>
.cropper-img {
	margin: auto;
	width: 100px;
	height: 100px;
	border-radius: 100%;
}
</style>
